import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {IndexService} from "../../services/index.service";
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {Subscription} from "rxjs";
@Component({
  selector: 'app-combine-table-settings',
  templateUrl: './combine-table-settings.component.html',
  styleUrls: ['./combine-table-settings.component.css']
})
export class CombineTableSettingsComponent implements OnInit, OnDestroy {

  subscribeData: Subscription;

  // 桌子分类列表
  tableClassList = [];

  // 并桌子列表
  tableList1 = [];

  // 合并桌子列表
  tableList2 = [];


  // 当前操作状态值
  tipType = 'error';
  isLoading = false;
  submitType = true;


  // 当前桌号信息
  currentTable = {
    tid: '', // 当前桌子id
    tableClass: '', //当前桌子分类
    tableName: '', //当前桌子名称
    orderNum: '', // 当前桌子流水号
    orderId: ''// 当前桌子订单id
  }

  // 并桌号
  combineTable = {
    selectCombineTableId: '',
    orderId: '',
    orderNum: ''
  }

  // 合并桌号
  unionTable = {
    selectUnionTableId: ''
  }

  constructor(private activatedRoute: ActivatedRoute,
              private indexService: IndexService,
              private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private router: Router) {
    this.activatedRoute.queryParams.subscribe(param => {
      this.currentTable.tid = param['tid'];
      this.currentTable.tableClass = param['tableClass'];
      this.currentTable.tableName = param['tableName'];
      this.currentTable.orderId = param['orderId'];
      this.currentTable.orderNum = param['orderNum'];
    });
  }

  /** ------------------------------------------------------------------------------并桌号操作开始-----------------------------------------------------------------------------* */

  // 并桌子分类切换
  combineChangClass($event) {
    let id = $event.target.value;
    this.tableList1 = [];
    this.combineTable.selectCombineTableId = '';
    this.tableClassList.forEach(v => {
      if (v.id == id) {
        this.tableList1 = v.tableList;
      }
    });
  }

  // 并桌子切换
  combineChangTable($event) {
    this.combineTable.selectCombineTableId = $event.target.value;

    this.combineTable.orderId = '';
    this.combineTable.orderNum = '';
    // 根据id查询订单

    this.extendHttp.post('/merchantApp/singleprocess/findOrderByTid', {tid: this.combineTable.selectCombineTableId}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.combineTable.orderId = data.orderForm.id;
        this.combineTable.orderNum = data.orderForm.order_id;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  /** ------------------------------------------------------------------------------并桌号操作结束-----------------------------------------------------------------------------* */

  /** ------------------------------------------------------------------------------合并桌号操作开始-----------------------------------------------------------------------------* */

  // 合并桌子分类切换
  unionChangClass($event) {
    let id = $event.target.value;
    this.tableList2 = [];
    this.unionTable.selectUnionTableId = '';
    this.tableClassList.forEach(v => {
      if (v.id == id) {
        this.tableList2 = v.tableList;
      }
    });
  }

  // 合并桌子切换
  unionChangTable($event) {
    this.unionTable.selectUnionTableId = $event.target.value;
  }

  /** ------------------------------------------------------------------------------合并桌号操作结束-----------------------------------------------------------------------------* */


  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.subscribeData = this.indexService.tableListEmit.subscribe(res => {
      this.tableClassList = res;
    });

    if (!this.tableClassList.length) {

      // 获取所有餐桌信息
      this.getTableList();
    }
  }

  getTableList() {
    this.extendHttp.post('/merchantApp/singleprocess/getAlltableList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tableClassList = data.list;
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  onSubmit() {
    if (!this.isLoading) {
      if (!this.combineTable.orderId.trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '没有查询到并桌订单ID');
        return;
      }

      if (!this.unionTable.selectUnionTableId.trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '请选择合并桌号');
        return;
      }
      this.isLoading = !this.isLoading;
      this.extendHttp.post('/merchantApp/singleprocess/mergetable', {
        tid: this.unionTable.selectUnionTableId,
        orderId1: this.currentTable.orderId,
        orderId2: this.combineTable.orderId
      }).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.submitType = false;
          this.isLoading = !this.isLoading;
          this.tipType = 'success';
          this.tipService.change(true, '并台成功');

          setTimeout(() => {
            this.router.navigate(['/main']);
          }, 1500);
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipType = 'error';
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }

  ngOnDestroy() {
    this.subscribeData.unsubscribe();
  }

}
